﻿using System;
using System.ComponentModel;
using System.Windows;
using Chambills.Present.Core;
using NUnit.Framework;
using Present.Commands.Async.Actions.Decorators;
using Rhino.Mocks;

namespace Present.Commands.Tests.Decorators
{
    [TestFixture]
    public class SuccessStateTransitionActionDecoratorTestCase : AsyncActionDecoratorTestCaseBase<SuccessStateTransitionActionDecorator<int, int>, int, int>
    {
        private IViewRegistry viewRegistry;
        private IPresentVisualStateManager statemanager;
        private readonly Action<int> onCompleted = x => { };
        private const string StateName = "State";

        public override void Setup()
        {
            statemanager = MockRepository.GenerateStub<IPresentVisualStateManager>();
            viewRegistry = MockRepository.GenerateStub<IViewRegistry>();
            base.Setup();
        }

        [Test]
        public void ParameterIsUsedAsViewModelIfNonePassedIn()
        {
           Decorator.HandleSuccess(1);
            viewRegistry.AssertWasCalled(x => x.FindViewFor(1));
        }

        [Test]
        public void ViewModelIsUsedToFindView()
        {
            var viewModel = MockRepository.GenerateStub<IViewModel>();
            Decorator = new SuccessStateTransitionActionDecorator<int, int>(Inner, statemanager, viewRegistry, viewModel, StateName, onCompleted);
            Decorator.HandleSuccess(1);
            viewRegistry.AssertWasCalled(x => x.FindViewFor(viewModel));
        }

        [Test, RequiresSTA]
        public void StateChanged()
        {
            var view = new FrameworkElement();
            viewRegistry.Stub(x => x.FindViewFor(1)).Return(view);
            Decorator.HandleSuccess(1);
            statemanager.AssertWasCalled(x => x.ChangeState(Arg<FrameworkElement>.Is.Equal(view), Arg<string>.Is.Equal(StateName), Arg<Action>.Is.Anything));
        }


        protected override SuccessStateTransitionActionDecorator<int, int> CreateDecorator()
        {
            return new SuccessStateTransitionActionDecorator<int, int>(Inner, statemanager, viewRegistry, null, StateName, onCompleted);
        }

        protected override int GetParameter()
        {
            return 1;
        }

        protected override int GetResult()
        {
            return 1;
        }
    }
}